package com.atguigu.lease.web.admin.schedule;

import com.atguigu.lease.model.entity.LeaseAgreement;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.admin.service.LeaseAgreementService;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class ScheduleTesks {

    @Autowired
    LeaseAgreementService leaseAgreementService;

    //cron表达式组成： 秒  分  时   日  月  周  年
    @Scheduled(cron = "0 0 0 * * ?") // 每个月每天不管周几，0点0分0秒时执行一次任务
    public void updateLeaseAgreementStatus(){
        LambdaUpdateWrapper<LeaseAgreement> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.lt(LeaseAgreement::getLeaseEndDate,new Date());
        updateWrapper.in(LeaseAgreement::getStatus, LeaseStatus.SIGNED,LeaseStatus.WITHDRAWING);
        updateWrapper.set(LeaseAgreement::getStatus,LeaseStatus.EXPIRED);
        //  update lease_agreement set status=4 where lease_end_date < now() and satus in (2,5)
        leaseAgreementService.update(updateWrapper);
    }
}
